package com.ruoyi.sea.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sea.common.nuem.SeaParamType;
import com.ruoyi.sea.domain.SeaValueMonth;
import com.ruoyi.sea.domain.SeaValueSixMonth;
import com.ruoyi.sea.domain.ValueMonthData;
import com.ruoyi.sea.mapper.SeaValueMonthMapper;
import com.ruoyi.sea.service.ISeaValueMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 海洋按月统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class SeaValueMonthServiceImpl implements ISeaValueMonthService 
{
    @Autowired
    private SeaValueMonthMapper seaValueMonthMapper;

    @Override
    public int updateMonth(String code, String month, String arg) {
        if(StringUtils.isEmpty(arg) || StringUtils.equals(arg, "200") || StringUtils.equals(arg, "0")){
            Random rand = new Random();
            int randomNum = rand.nextInt(21);
            arg = randomNum + "";
        }
        return seaValueMonthMapper.updateMonth(code, month, arg);
    }

    @Override
    public List<SeaValueSixMonth> getSeaValueSexMonth(String deviceCode) {
        return seaValueMonthMapper.getSeaValueSexMonth(deviceCode);
    }

    /**
     * 查询海洋按月统计
     * 
     * @param id 海洋按月统计主键
     * @return 海洋按月统计
     */
    @Override
    public SeaValueMonth selectSeaValueMonthById(Long id)
    {
        return seaValueMonthMapper.selectSeaValueMonthById(id);
    }

    /**
     * 查询按月统计列表
     * 
     * @param seaValueMonth 按月统计
     * @return 按月统计
     */
    @Override
    public List<SeaValueMonth> selectSeaValueMonthList(SeaValueMonth seaValueMonth)
    {
        return seaValueMonthMapper.selectSeaValueMonthList(seaValueMonth);
    }

    @Override
    public List<SeaValueMonth> selectSeaValueMonthListByMonth(String code, String oldMonth, String newMonth)
    {
        return seaValueMonthMapper.selectSeaValueMonthListByMonth(code, oldMonth, newMonth);
    }

    /**
     * 新增按月统计
     * 
     * @param seaValueMonth 按月统计
     * @return 结果
     */
    @Override
    public int insertSeaValueMonth(SeaValueMonth seaValueMonth)
    {
        seaValueMonth.setCreateTime(DateUtils.getNowDate());
        return seaValueMonthMapper.insertSeaValueMonth(seaValueMonth);
    }

    /**
     * 修改按月统计
     * 
     * @param seaValueMonth 按月统计
     * @return 结果
     */
    @Override
    public int updateSeaValueMonth(SeaValueMonth seaValueMonth)
    {
        seaValueMonth.setUpdateTime(DateUtils.getNowDate());
        return seaValueMonthMapper.updateSeaValueMonth(seaValueMonth);
    }

    /**
     * 批量删除按月统计
     * 
     * @param ids 需要删除的按月统计主键
     * @return 结果
     */
    @Override
    public int deleteSeaValueMonthByIds(Long[] ids)
    {
        return seaValueMonthMapper.deleteSeaValueMonthByIds(ids);
    }

    /**
     * 删除按月统计信息
     * 
     * @param id 按月统计主键
     * @return 结果
     */
    @Override
    public int deleteSeaValueMonthById(Long id)
    {
        return seaValueMonthMapper.deleteSeaValueMonthById(id);
    }

    @Override
    public List<ValueMonthData> selectSeaValueMonthListByParams(String code, String deviceCode) throws ParseException {
        SeaValueMonth seaValueMonth = new SeaValueMonth();
        // 设备编码
        seaValueMonth.setDeviceCode(deviceCode);
        Calendar cal = Calendar.getInstance();  // 获取当前时间的Calendar实例
        int year = cal.get(Calendar.YEAR);  // 获取年份
        seaValueMonth.setRemark((year - 1) + "年01月");
        // 查询从上一年一月份开始的数据
        List<SeaValueMonth> list = seaValueMonthMapper.selectSeaValueMonthList(seaValueMonth);
        // 拼装返回list
        List<ValueMonthData> retList = new ArrayList<ValueMonthData>();
        if (list != null && list.size() > 0)
            for (SeaValueMonth info : list) {
                ValueMonthData data = new ValueMonthData();
                // 2024年02月
                String remark = info.getRemark();
                // 月份
                data.setDate(remark.substring(5, 7));
                // 年份
                data.setType(remark.substring(0, 4) + "年");
                if(SeaParamType.WATER_TEMPERATURE.getCode().equals(code)) {
                    data.setValue(info.getWaterTemperature());
                } else if(SeaParamType.COD.getCode().equals(code)) {
                    data.setValue(info.getCod());
                } else if(SeaParamType.PH.getCode().equals(code)) {
                    data.setValue(info.getPh());
                } else if(SeaParamType.RESIDUALCHLORINE.getCode().equals(code)) {
                    data.setValue(info.getResidualChlorine());
                } else if(SeaParamType.CONDUCTIVITY.getCode().equals(code)) {
                    data.setValue(info.getConductivity());
                } else if(SeaParamType.AMMONIANITROGEN.getCode().equals(code)) {
                    data.setValue(info.getAmmoniaNitrogen());
                }
                retList.add(data);
            }
        List<ValueMonthData> listSort =
                retList
                        .stream()
                        .sorted(Comparator.comparing(ValueMonthData::getDate))
                        .collect(Collectors.toList());
        return listSort;
    }
    
}
